package indi.gao.leetcode;

/**
 * @Description: 约瑟夫环
 * 循环删除第 n 个元素 求最后剩下的元素
 * @create: 2020/3/31 22:55
 * @update logs
 * @throws Exception
 */
public class simlpe {
    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.lastRemaining(5, 3));
    }
}

class Solution {
    // n 多少个元素 m删除第几个元素
    public int lastRemaining(int n, int m) {
        int result = 0;
        for (int i = 1; i < n; i++) {
            result = (result + m) % (i + 1);
        }
        return result;
    }
}
